package jbrowser.session;

import java.io.IOException;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import jbrowser.JBrowser;

public class SessionStore {

    HashMap<String, Session> store = new HashMap<String, Session>();

    public int size() {
        return store.size();
    }

    public boolean isEmpty() {
        return store.isEmpty();
    }

    public Session getSession(URI uri) {
        return getSession(uri, true);
    }

    public Session getSession(URI uri, boolean createIfNotExists) {
        Session session = null;
        try {
            SessionFactory factory =null;// SessionFactoryProvider.lookupForProtocol(uri.getScheme());
            if (factory != null) {
                session = factory.getSession(uri);
            } else if (uri.getScheme().equals("jar")) {
                JarURLConnection jarURLConnection = (JarURLConnection) uri.toURL().openConnection();
                session = store.get(jarURLConnection.getJarFileURL().toString());
                if (session == null) {
                    Logger.getLogger(SessionStore.class.getName()).log(Level.SEVERE, "Create new JarSession");
                    session = new JarSession(uri);
                }
            } else if (uri.getScheme().equals("file")) {
            } else if (uri.getScheme().equals("http")) {
            } else if (uri.getScheme().equals("app")) {
//                String path = "jar:" + uri.toString().replaceAll("app://", JBrowser.getRepository().toURI().toString());
//                URL url = new URL(path);
//                JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();
//                session = store.get(jarURLConnection.getJarFileURL().toString());
//                if (session == null) {
//                    Logger.getLogger(SessionStore.class.getName()).log(Level.SEVERE, "Create new AppSession");
//                    session = new AppSession(url.toURI());
//                }           
            } else {
            }
        } catch (MalformedURLException ex) {
            Logger.getLogger(SessionStore.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SessionStore.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (session != null && !contains(session)) {
            add(session);
        }
        Logger.getLogger(SessionStore.class.getName()).log(Level.SEVERE, "Return Session: {0}", session);
        return session;
    }

    public Session remove(Session session) {
        return store.remove(session.getHost());
    }

    public void clear() {
        store.clear();
    }

    public boolean contains(Session session) {
        return store.containsValue(session);
    }

    public Set<String> getHosts() {
        return store.keySet();
    }

    public Collection<Session> getSessions() {
        return store.values();
    }

    public Session add(Session session) {
        return store.put(session.getHost(), session);
    }
}
